﻿// ***********************************************************************
// Assembly         : OpenAuth.Domain
// Author           : yubaolee
// Created          : 10-25-2015
//
// Last Modified By : yubaolee
// Last Modified On : 10-25-2015
// ***********************************************************************
// <copyright file="IRepository.cs" company="www.cnblogs.com/yubaolee">
//     Copyright (c) www.cnblogs.com/yubaolee. All rights reserved.
// </copyright>
// <summary>仓储接口</summary>
// ***********************************************************************

using System;
using System.Linq;
using System.Linq.Expressions;

namespace OpenAuth.Domain.Interface
{
    /// <summary>
    /// 仓储层公共接口，定义了公共方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IRepository<T> where T : class
    {


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="exp">查询表达式</param>
        /// <param name="expOrderBy">排序表达式</param>
        /// <returns></returns>
        IQueryable<T> Find(Expression<Func<T, bool>> exp = null, Expression<Func<T, string>> expOrderBy = null);
        /// <summary>
        /// 查询实体是否存在
        /// </summary>
        /// <param name="exp">查询表达式</param>
        /// <returns></returns>
        bool IsExist(Expression<Func<T, bool>> exp);
        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        T FindSingle(Expression<Func<T, bool>> exp = null);
        /// <summary>
        /// 得到分页记录
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="orderby"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        IQueryable<T> Find(int pageindex = 1, int pagesize = 10, string orderby = "", Expression<Func<T, bool>> exp = null);
        /// <summary>
        /// 根据过滤条件获取记录数
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        int GetCount(Expression<Func<T, bool>> exp = null);
        /// <summary>
        /// 添加单个实体
        /// </summary>
        /// <param name="entity"></param>
        void Add(T entity);
        /// <summary>
        /// 批量增加实体
        /// </summary>
        /// <param name="entities"></param>
        void BatchAdd(T[] entities);
        /// <summary>
        /// 更新一个实体的所有属性
        /// </summary>
        void Update(T entity);
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        void Delete(T entity);

        /// <summary>
        /// 按指定的ID进行批量更新
        /// </summary>
        void Update(Expression<Func<T, object>> identityExp, T entity);

        /// <summary>
        /// 实现按需要只更新部分更新
        /// <para>如：Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
        /// </summary>
        /// <param name="where">更新条件</param>
        /// <param name="entity">更新后的实体</param>
        void Update(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity);
        /// <summary>
        /// 批量删除
        /// </summary>
        void Delete(Expression<Func<T, bool>> exp);

        void Save();
    }
}